package com.zjuee.web.auth.controller;

import com.zjuee.base.bean.response.R;
import com.zjuee.base.constant.Common;
import com.zjuee.base.model.common.auth.UserRole;
import com.zjuee.service.auth.service.UserRoleService;
import com.zjuee.service.auth.vo.MenuVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户角色 web 接口
 *
 * @author wu liang
 * @since 1.0
 */
@Slf4j
@RestController
@RequestMapping(path = Common.Service.ZJUEE_AUTH_USER_ROLE_URL_PREFIX, name = Common.Service.ZJUEE_AUTH_SERVICE_NAME)
public class UserRoleController {

    @Resource
    private UserRoleService userRoleService;


    //获取当前用户已分配的所有角色(列表形式)
    @GetMapping("/roles/{userId}")
    public R<UserRole> getUserRole(@PathVariable("userId") Long userId) {
        try {
            UserRole userRole = userRoleService.getUserRole(userId);
            if (null != userRole) {
                return R.ok(userRole);
            }
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
        return R.fail();
    }

    //查询用户权限 - 树形结构
    @GetMapping("/menus/user")
    public R<List<MenuVo>> getUserMenus(@RequestParam("userId") Long userId) {
        try {
            List<MenuVo> menuList = userRoleService.findAllMenus(userId);
            if (null != menuList) {
                return R.ok(menuList);
            }
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
        return R.fail();
    }

}
